package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.Volunteer;
import com.ruoyi.system.domain.deptSchool;
import com.ruoyi.system.service.VolunteerService;
import com.ruoyi.system.service.deptSchoolService;
import com.ruoyi.system.service.impl.deptSchoolServiceIml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeoutException;

@RestController
@RequestMapping("/system/collage_department")
public class deptSchoolController extends BaseController {
    @Autowired
    private deptSchoolService deptSchoolService;
    @Autowired
    private VolunteerService volunteerService;
    @PreAuthorize("@ss.hasPermi('system:collage_department:list')")
    @GetMapping("/list")
    public TableDataInfo getList(deptSchool deptSchool,String grade){
        int grades=0;
        if (grade!=null) {
            grades = Integer.parseInt(grade);
        }
        startPage();
        List<deptSchool> deptSchools = deptSchoolService.selectDeptSchool(deptSchool,grades);
        return getDataTable(deptSchools);
    }
    @PreAuthorize("@ss.hasPermi('system:collage_department:add')")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody Volunteer volunteer) throws TimeoutException {
        Volunteer volunteers1 = volunteerService.selectVolunteerOne(volunteer);
        System.out.println(volunteers1);
        if (volunteers1==null){
            return toAjax(volunteerService.insertVolunteer(volunteer));
        }else {
            throw new TimeoutException(volunteers1.getsName()+"   "+volunteers1.getdName() +"已存在");
        }
    }
    @PreAuthorize("@ss.hasPermi('system:collage_department:addList')")
    @PostMapping("/addList")
    public AjaxResult addList(@RequestBody List<Volunteer> volunteers)throws TimeoutException{
        System.out.println(volunteers);
        List<Volunteer> volunteers1 = new LinkedList<Volunteer>();
        String volunteer_throw= new String();
        for (Volunteer volunteer:volunteers) {
            if (volunteerService.selectVolunteerOne(volunteer)!=null){
                volunteers1.add(volunteerService.selectVolunteerOne(volunteer));
            }
            if (volunteerService.selectVolunteerOne(volunteer)==null){
                volunteerService.insertVolunteer(volunteer);
            }
        }
        System.out.println(volunteers1);
        if (!volunteers1.isEmpty()) {
            volunteer_throw=" ";
            for (Volunteer volunteer : volunteers1) {
                volunteer_throw += volunteer.getsName();
                volunteer_throw += "   ";
                volunteer_throw += volunteer.getdName();
                volunteer_throw += "   ";
            }
            if (volunteer_throw != null && volunteer_throw != " ") {
                System.out.println(volunteer_throw);
                throw new TimeoutException(volunteer_throw+"已存在");
            }
        }
        return toAjax(1);
    }
    @PreAuthorize("@ss.hasPermi('system:collage_department:disappear')")
    @PostMapping("/disappear/{collage_department_id}")
    public TableDataInfo disappear(@RequestBody List<deptSchool> deptSchools,@PathVariable int collage_department_id){
        System.out.println(deptSchools);
        System.out.println(collage_department_id);
        Iterator<deptSchool> iterable=deptSchools.iterator();
        while (iterable.hasNext()){
            deptSchool next = iterable.next();
            System.out.println(next.getCollage_department_id());
            if (next.getCollage_department_id() == collage_department_id) {
                iterable.remove();
            }
        }
        startPage();
        return getDataTable(deptSchools);
    }
    @GetMapping("/find/{c_name}")
    public TableDataInfo findBYSchool(@PathVariable String c_name){
        String d_name=null;
        List<deptSchool> deptSchools = deptSchoolService.selectBySchool(c_name, d_name);
        return getDataTable(deptSchools);
    }

}
